/**
 * Mar 19, 2014 1:56:30 PM
 */
package elitech.vietnam.weatherfashion.tabs.settings;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.ultilities.ValueOption;
import elitech.vietnam.weatherfashion.widget.NotifyButton;

/**
 * @author Cong
 *
 */
public class NotificationSettingsFragment extends AbstractFragment implements OnCheckedChangeListener, OnClickListener {

	CheckBox mCBEnable, mCBFriendPost, mCBMyStyle, mCBReply, mCBFriendAddMe, mCBSound;
	Button mButtonBack;
	ValueOption option;
	NotifyButton button_notification;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.settings_notification, container, false);
		setupView(view);
		
		mCBEnable = (CheckBox) view.findViewById(R.id.stnoti_cbEnable);
		mCBFriendPost = (CheckBox) view.findViewById(R.id.stnoti_cbFriendPost);
		mCBMyStyle = (CheckBox) view.findViewById(R.id.stnoti_cbMystyleComment);
		mCBReply = (CheckBox) view.findViewById(R.id.stnoti_cbReplyComment);
		mCBFriendAddMe = (CheckBox) view.findViewById(R.id.stnoti_cbFriendAddMe);
		mCBSound = (CheckBox) view.findViewById(R.id.stnoti_cbEnableSound);
		mButtonBack = (Button) view.findViewById(R.id.stnoti_button_back);
		button_notification = (NotifyButton) view.findViewById(R.id.button_notification);
		
		((MainActivity) getActivity()).setCurrentNotifyButton(button_notification);

		MemberDTO m = GlobalValue.getInstance(getActivity()).readUserInfo();
		if (m == null)
			button_notification.setVisibility(View.GONE);
		else 
			button_notification.setVisibility(View.VISIBLE);
		//button_notification.setOnClickListener(this);

		option = GlobalValue.getInstance(getActivity()).readOptionInfo();
		mCBEnable.setChecked(option.isGetNotification());
		mCBFriendPost.setChecked(option.isType3);
		mCBMyStyle.setChecked(option.isType1);
		mCBReply.setChecked(option.isType2);
		mCBFriendAddMe.setChecked(option.isType4);
		mCBSound.setChecked(option.isNotiSound);
		
		mButtonBack.setOnClickListener(this);
		mCBEnable.setOnCheckedChangeListener(this);
		mCBFriendPost.setOnCheckedChangeListener(this);
		mCBMyStyle.setOnCheckedChangeListener(this);
		mCBReply.setOnCheckedChangeListener(this);
		mCBFriendAddMe.setOnCheckedChangeListener(this);
		mCBSound.setOnCheckedChangeListener(this);
		
		return view;
	}
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) { 
		case R.id.stnoti_cbEnable: 
			option.setGetNotification(isChecked);
			mCBFriendPost.setEnabled(isChecked);
			mCBMyStyle.setEnabled(isChecked);
			mCBReply.setEnabled(isChecked);
			mCBFriendAddMe.setEnabled(isChecked);
			if (isChecked) {
				mCBFriendPost.setChecked(isChecked); option.isType3 = isChecked;
				mCBMyStyle.setChecked(isChecked); option.isType1 = isChecked;
				mCBReply.setChecked(isChecked); option.isType2 = isChecked;
				mCBFriendAddMe.setChecked(isChecked); option.isType4 = isChecked;
			}
			break;
		case R.id.stnoti_cbEnableSound:
			option.isNotiSound = isChecked;			
			break;
		case R.id.stnoti_cbFriendPost:
			option.isType3 = isChecked;
			break;
		case R.id.stnoti_cbMystyleComment:
			option.isType1 = isChecked;
			break;
		case R.id.stnoti_cbReplyComment:
			option.isType2 = isChecked;
			break;
		case R.id.stnoti_cbFriendAddMe:
			option.isType4 = isChecked;
			break;
		default:
			break;
		}
		GlobalValue.getInstance(getActivity()).writeOptionInfo(option);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.stnoti_button_back:
			getActivity().onBackPressed();
			break;
		case R.id.button_notification:
			Log.w("Notification", "Num Count: " + WFApplication.getInstance().mListNoti.size());
			((MainActivity) getActivity()).mNotiWindow.showAsDropDown(button_notification);
			((MainActivity) getActivity()).mNotiWindow.notifyDataChanged();
			button_notification.setNumber(0);
			break;
		default:
			break;
		}
	}

	
}
